<?php

namespace App\Console\Commands;

use App\Models\Base\ServerNotice;
use App\Service\RequestService;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class ServerNoticePlan extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'server:noticePlan';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '定时公告';

    /**
     * Create a new command instance.
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $now = CarBon::now();
        $now_time = $now->format('Y-m-d H:i');
        $now_timestamp = strtotime($now_time);
        $where = [
            ['notice_type', '=', 2],
            ['start_time', '<=', $now_time],
            ['end_time', '>=', $now_time],
            ['frequency', '>', 0],
        ];
        $data = ServerNotice::where($where)->get();
        if ($data->isEmpty()) {
            return false;
        }
        foreach ($data as $key => $value) {
            $frequency = $value->frequency * 60; //执行频率
            $now_num = $value->time_num;
            $update_time = Carbon::parse($value->updated_at)->format('Y-m-d H:i');
            $start_time = strtotime($update_time);

            if (0 == $value->time_num && $start_time < $now_timestamp) {
                $ret_notice = $this->sendNotice($value->server_id, $value->site_type, $value->content);
                if ($ret_notice) {
                    $this->updateTimeNumber($value->id, $now_num);
                }
            } else {
                $next_time = $start_time + $frequency;

                if ($next_time != $now_timestamp) {
                    return false;
                }
//                Log::error('timesxxxxxxxxxxx', [$start_time, $frequency]);
                $ret_notice = $this->sendNotice($value->server_id, $value->site_type, $value->content);
                $this->updateTimeNumber($value->id, $now_num);
            }
        }
    }

    /**
     * [sendNotice 发送公告].
     *
     * @Author   王文凡
     * @DateTime 2018-05-18
     *
     * @version  1.0
     *
     * @param [type] $server_id [description]
     * @param [type] $type      [description]
     * @param [type] $content   [description]
     *
     * @return [type] [description]
     */
    protected function sendNotice($server_id, $type, $content)
    {
        $option = $type.';'.$content.'; ';
        $ret = RequestService::sendNotice($server_id, $option);
        if ($ret) {
            return true;
        } else {
//            Log::error('发送公告失败', ['server_id' => $server_id, 'type' => $type, 'content' => $content]);

            return false;
        }
    }

    /**
     * [updateTimeNumber 更新本地数据].
     *
     * @Author   王文凡
     * @DateTime 2018-05-18
     *
     * @version  1.0
     *
     * @param [type] $id      [description]
     * @param [type] $now_num [description]
     *
     * @return [type] [description]
     */
    protected function updateTimeNumber($id, $now_num)
    {
        ++$now_num;
        $ret = ServerNotice::where('id', $id)->update(['time_num' => $now_num]);
        if (!$ret) {
            Log::error('更新公告数据失败', ['id' => $id, 'now_num' => $now_num]);

            return false;
        }

        return true;
    }
}
